<template>

	<view class="mainbgc" v-if="pageShow">
		<u-popup round="20" :show="show" @close="show=false">
			<view class="mainpadding">
				<view class="flexcenter">
					<view class="titletext">筛选</view>
				</view>
				<view class="strongtext margin_top">时间</view>
				<view class="flexleft" style="flex-wrap: wrap;">
					<view class="screenbox flexcenter" v-for="(item,index) in datelist" :key="item.id"
						@click="change(item.name,index)">
						<view :class="currents == index? 'itemscreenfocus margin_top':'itemscreen margin_top'">
							{{item.name}}
						</view>
					</view>
				</view>
				<view class="strongtext margin_top">自定义时间</view>
				<view class="flexleft">
					<view class="inputscreenbox flexcenter" @click="shows=true">
						<input class="itemscreen margin_top" type="text" disabled v-model="start">
					</view>
					<view class="zwscreenbox flexcenter">
						<view class="strongtext">-</view>
					</view>
					<view class="inputscreenbox flexcenter" @click="showss=true">
						<input class="itemscreen margin_top" type="text" disabled v-model="end">
					</view>
				</view>
				<view class="flexbetween selectboxtop2 margin_top"
					@click="khDengJiShow=true">
					<input type="text" placeholder="点击选择客户等级" disabled="true" v-model="khDengJiText">
					<view class="righticon2"></view>
				</view>
				<view class="flexbetween selectboxtop2 margin_top"
					@click="selectRole('/pages/workbench/jiben/yuangongda?type=2')">
					<input type="text" placeholder="选择经办人" disabled="true" v-model="staff_name">
					<view class="righticon2"></view>
				</view>
				<view class="flexbetween">
					<view class="surebtn reset flexcenter" style="margin-top: 60rpx;" @click="resetbtn">重置</view>
					<view class="surebtn flexcenter" style="margin-top: 60rpx;" @click="sureBtn">确认</view>
				</view>
			</view>
		</u-popup>
		<header-top :messageInfo="{typeFlag:true,message:'客户预收款查询',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexleft" @click="show=true">筛选</view>
			</view>
		</header-top>
		<view class="mainpadding">
			<view class="flexbetween">
				<input class="input-text" type="text" placeholder="请输入搜索内容" v-model="keywords">
				<view class="sousuo" @click="init()">搜索</view>
			</view>
			<view class="flexcenter" v-if="!list.data.length">
				<view class="placeholderimage"></view>
			</view>
			<view class="" style="padding-bottom: 120rpx;">
				<view class="mainpadding_top ffffff radius flexleft" v-for="item in list.data" :key="item.id" @click="jumpdetail(item.id)">
					<view class="bigimg" style="margin: 0;">
						<view class="nostrongtext margin_right">{{item.order_no}}</view>
						<view class="margin_top2 flexbetween">
							<view class="flexleft">
								<view class="titletext">{{item.custom.name}}</view>
								<view class="namecolor" style="margin-left: 20rpx;" @click.stop="phone(item.custom.mobile)">{{item.custom.username}}</view>
								<view class="flexright">
									<view class="littleblue"></view>
								</view>
							</view>
							<view class="margin_top2 ywy" v-if="item.custom.costom_level_info">{{item.custom.costom_level_info.name}}</view>
						</view>
						<view class="margin_top2 ywy flexbetween">
							<view class="flexstart" @click.stop="phone(item.staff.mobile)">
								<view class="">经办人：{{item.staff.username}}</view>
								<view class="dizhiimg"></view>
							</view>
							<view>{{item.createtime_text}}</view>
						</view>
						<view class="margin_top2 ywy nostrongtext yihang">地址：{{item.custom.cityinfo}}{{item.custom.address}}</view>
						<view class="margin_top2 ywy nostrongtext yihang" v-show="item.notes">备注：{{item.notes}}</view>
						<view class="flexleft margin_top2 nostrongtext">
							<view class="margin_right ywy">金额：<text class="namecolor">{{item.pay_price}}</text></view>
						</view>
					</view>
				</view>
			</view>
			<view class="posibottom" style="bottom: 30rpx;">
				<view class="onebox flexcenter flex_wrap">
					<view class="huangse">共{{dataAll.data.total}}笔，</view>
					<view class="huangse" style="color: #ff0000;">总预收款{{dataAll.total_price}}元</view>
				</view>
			</view>
		</view>
		<u-datetime-picker :show="shows" v-model="value1" mode="date" @confirm="confirm" @cancel="shows=false"
			closeOnClickOverlay @close="shows=false"></u-datetime-picker>
		<u-datetime-picker :show="showss" v-model="value2" mode="date" @confirm="confirms" @cancel="showss=false"
			:minDate="mintime" @close="showss=false"></u-datetime-picker>
			<selectKHDengJi :message="{khDengJiShow:khDengJiShow}" @khDengJiSure="khDengJiSure"></selectKHDengJi><!-- 客户等级 -->
	</view>
	
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				keywords: "",
				page: 1,
				limit: 12,
				list: {
					data: [],
					is_all: false
				},
				show: false,
				// 时间选择
				datelist: [{
						id: 1,
						name: '今日'
					},
					{
						id: 2,
						name: '昨日'
					},
					{
						id: 3,
						name: '本月'
					},
				],
				pageShow:false,
				// 时间筛选
				currents: -1,
				currentstatus: -1,
				labledj: -1,
				start: '开始时间',
				end: '结束时间',
				shows: false,
				showss: false,
				value1: Number(new Date()),
				value2: Number(new Date()),
				mintime: Number(new Date()),
				maxtime: Number(new Date()),
				dataAll:{},
				staff_id:"",
				staff_name:"",
				// 客户等级
				khDengJiShow:false,
				khDengJiText:"",
				custom_level_id:"",//客户等级ID
			}
		},
		onLoad() {
			this.init()
		},
		onShow() {
			uni.$off('ygSelectFn');
			uni.$on('ygSelectFn', (data) => {
				this.staff_id = data.id
				this.staff_name = data.name
				return false
			})
		},
		watch: {
			list: {
				deep: true,
				handler(newval) {
					let num = 0
					this.list.data.forEach(item => {
						if (item.is_check) {
							num++
						}
					})
					if (this.list.data.length == num) {
						this.list.is_all = true
					} else {
						this.list.is_all = false
					}
				}
			}
		},
		methods: {
			khDengJiSure(e){//客户等级
				if (e) {
					this.khDengJiText = e.value[0].name
					this.custom_level_id = e.value[0].id
				}
				this.khDengJiShow = false
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/dhtapi/Customyskorder/index', 'GET', {
					page: page || 1,
					limit: _this.limit,
					keywords: this.keywords,
					start_time: this.start == '开始时间' ? '' : this.start, //开始日期
					end_time: this.end == '结束时间' ? '' : this.end, //结束日期
					custom_level_id:this.custom_level_id,
					staff_id:this.staff_id,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data.data;
						let dataList = _this.list;
						this.$set(res.data.data,'is_all',false)
						res.data.data.data.forEach(item=>{
							this.$set(item,'is_check',false)
						})
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							_this.list = resList
							_this.list.data = resList.data;
						}
					}
					this.pageShow = true
					this.dataAll = res.data
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			jumpdetail(id) {
				uni.navigateTo({
					url: '/pages/workbench/caiwu/kehuyskxq?id=' + id
				})
			},
			submit() {
				let arr = []
				this.list.data.forEach(item => {
					if (item.is_check) {
						arr.push(item.id)
					}
				})
				if (arr.length == 0) {
					httpRequest.toast("请选择需要汇总的数据")
					return
				}
				console.log(arr, "统计数据")
				let data = {
					type: 1, //1退货单，2采购单，3采购退货单，4入库单，5销售单
					arr: arr
				}
				uni.navigateTo({
					url: '/pages/workbench/xiaoshou/tuihuodtjmx?data=' + encodeURIComponent(JSON.stringify(data))
				})
			},
			phone(p) {
				uni.makePhoneCall({
					phoneNumber: p.toString()
				}).catch((e) => {
					// console.log(e)  //用catch(e)来捕获错误{makePhoneCall:fail cancel}
				})
			},
			date(timer) {
				var date = new Date(timer); //当前时间
				var year = date.getFullYear() //返回指定日期的年份
				var month = repair(date.getMonth() + 1); //月
				var day = repair(date.getDate()); //日
				var curTime = year + "-" + month + "-" + day

				function repair(i) {
					if (i >= 0 && i <= 9) {
						return "0" + i;
					} else {
						return i;
					}
				}
				return curTime
			},
			confirm(e) {
				console.log(e);
				this.shows = false;
				this.show = true;
				this.end = '结束时间';
				this.mintime = e.value;
				this.start = this.date(e.value);
				this.currents = -1;
			},
			confirms(e) {
				this.showss = false;
				this.show = true;
				this.maxtime = e.value;
				this.end = this.date(e.value);
				this.currents = -1;
			},
			changestatus(index) {
				this.currentstatus = index;
			},
			changeslable(index) {
				this.labledj = index;
			},
			change(name, index) {
				if (name == "今日") {
					this.start = this.riqi(0);
					this.end = this.riqi(0);
				} else if (name == "昨日") {
					this.start = this.riqi(-1);
					this.end = this.riqi(-1);
				} else if (name == "本月") {
					this.start = this.benyue();
					this.end = this.riqi(0);
				}
				this.currents = index;
			},
			// 昨天“-1”今天“0”明天“1”
			riqi(day) {
				let dd = new Date();
				dd.setDate(dd.getDate() + day);
				let y = dd.getFullYear();
				let m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
				let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
				return y + "-" + m + "-" + d;
			},
			// 本月第一天
			benyue() {
				let y = new Date().getFullYear(); //获取年份
				let m = new Date().getMonth() + 1; //获取月份
				let d = '01'
				m = m < 10 ? '0' + m : m; //月份补 0
				return [y, m, d].join("-")
			},
			// 重置
			resetbtn() {
				this.currentstatus = -1
				this.currents = -1
				this.start = "开始时间"
				this.staff_id = ""
				this.staff_name = ""
				this.khDengJiText = ""
				this.custom_level_id = ""
				this.end = "结束时间"
				this.init()
				this.show = false
			},
			selectRole(url) {
				uni.navigateTo({
					url
				})
			},
			sureBtn() {
				this.show = false
				this.init()
			}
		}
	}
</script>
<style lang="scss" scoped>
	.seletext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
		line-height: 28rpx;
	}

	.miancolor {
		color: #0BBDCB;
	}

	.smalltext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.ywy {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
.clable{
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FAA500;
		line-height: 28rpx;
		background-color: #f6eddd;
	}
	.hlable{
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FD463E;
		line-height: 28rpx;
		background-color: #f6e4e3;
	}

	.wlable {
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #F3AD3D;
		line-height: 28rpx;
		background-color: #fef7ec;
	}

	.needxiahua {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
		line-height: 28rpx;
		border-bottom: 1rpx solid #0BBDCB;
	}

	.needbotborder {
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #e9e9e9;
	}

	.pagebottom {
		width: 100%;
		padding: 30rpx 30rpx 40rpx;
		box-sizing: border-box;
		background-color: #fff;
		position: fixed;
		bottom: 0;
	}

	.pagebottom_left {
		margin-left: 40rpx;
	}

	.pagebottom_right {
		margin-right: 100rpx;
	}

	.pagebottom_fahuo {
		color: #fff;
		padding: 10rpx 80rpx;
		font-size: 28rpx;
		border-radius: 30rpx;
		background-color: #0BBDCB;
		background-color: #00B855;
	}

	.allchange {
		margin-left: 20rpx;
	}

	.bigimg {
		width: 100%;
		margin-left: 30rpx;
	}

	.dizhitxt {
		font-size: 32rpx;
		color: #0BBDCB;
		font-weight: bold;
	}

	.dizhi {
		margin: 0 20rpx;
	}

	// 审核状态
	.wlable {
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #F3AD3D;
		line-height: 28rpx;
		background-color: #fef7ec;
	}

	.shlable {
		padding: 6rpx 14rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
		line-height: 28rpx;
		background-color: #e7f9fa;
	}

	.inputscreenbox {
		width: 40%;

		input {
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			border-radius: 50rpx;
			font-size: 28rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333;
			background-color: #f7f9fa;
		}
	}

	.zwscreenbox {
		width: 20%;
		line-height: 80rpx;
	}

	.itemscreenfocus {
		width: 200rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 50rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
		background-color: #e7f9fa;
	}

	// 筛选内容
	.screenbox {
		width: 33.33%;
	}

	.itemscreen {
		width: 200rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 50rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
		background-color: #f7f9fa;
	}

	.surebtn {
		width: 45%;
	}

	.reset {
		background: #ebb563;
	}

	// 筛选内容
	.screenbox {
		width: 33.33%;
	}

	.itemscreen {
		width: 200rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 50rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
		background-color: #f7f9fa;
	}

	.surebtn {
		width: 45%;
	}

	.reset {
		background: #ebb563;
	}
</style>